<?php
namespace app\beautiful\controller\admin\logs;
use app\beautiful\model\logs\MemberBalanceLogsModel;
use app\beautiful\model\logs\MemberCouponLogsModel;
use app\beautiful\model\logs\MemberIntegralLogsModel;
use app\beautiful\model\member\MemberCouponModel;
use app\beautiful\model\member\MemberModel;
use app\beautiful\model\order\BuyOrderModel;
use app\BaseController;
use app\common\logic\DataLogic;

class Member extends BaseController{
    public function coupon()
    {
        $member_id =$this->request->param('member_id/d',0);
        $nick_name=$this->request->param('nick_name');
        $mobile=$this->request->param('mobile');
        $type = $this->request->param('type/d');
        $where=[
            ['shop_id','=',$this->request->shopId]
        ];

        //会员判断
        $member_where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0]
        ];
        if(!empty($member_id)) $member_where[]=['member_id','=',$member_id];
        if(!empty($nick_name)) $member_where[]=['nick_name','=',$nick_name];
        if(!empty($mobile)) $member_where[]=['mobile','=',$mobile];
        $member_ids=MemberModel::where($member_where)->column('member_id') ?? [];
        $where[]=['member_id','in',$member_ids];

        //优惠券判断
        if(!empty($type) && in_array($type,[1,-1])) $where[]=['type','=',$type];

        $member_coupon_logs=new MemberCouponLogsModel();
        $member_coupon_logs=$member_coupon_logs->with(['membercoupon'=>function($query){
            $query->field('member_coupon_id,name,coupon_money,status');
        },'member']);
        $order=['add_time'=>'desc'];
        $field="*";
        $append=['add_time_format'];
        $return = DataLogic::getDataList($member_coupon_logs,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function balance()
    {
        $member_id =$this->request->param('member_id/d',0);
        $nick_name=$this->request->param('nick_name');
        $mobile=$this->request->param('mobile');
        $operate_time = $this->request->param('operate_time/a');
        $where=[
            ['shop_id','=',$this->request->shopId]
        ];

        //会员判断
        $member_where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0]
        ];
        if(!empty($member_id)) $member_where[]=['member_id','=',$member_id];
        if(!empty($nick_name)) $member_where[]=['nick_name','=',$nick_name];
        if(!empty($mobile)) $member_where[]=['mobile','=',$mobile];
        $member_ids=MemberModel::where($member_where)->column('member_id') ?? [];
        $where[]=['member_id','in',$member_ids];

        //操作时间判断
        if(!empty($operate_time) && count($operate_time) == 2){
            $operate_bg_time=$operate_time[0];
            $operate_end_time=$operate_time[1];
            if(!empty($operate_bg_time) || !empty($operate_end_time)){
                if(false !== $operate_bg_time_unix = strtotime($operate_bg_time." 00:00:00")){
                    if(false !== $operate_end_time_unix = strtotime($operate_end_time." 23:59:59")){
                        $where[]=['add_time','between',[$operate_bg_time_unix,$operate_end_time_unix]];
                    }else{
                        $where[]=['add_time','>=',$operate_bg_time_unix];
                    }
                }else{
                    if(false !== $operate_end_time_unix = strtotime($operate_end_time." 23:59:59")){
                        $where[]=['add_time','<=',$operate_bg_time_unix];
                    }
                }
            }

        }

        $member_balance_logs=new MemberBalanceLogsModel();
        $member_balance_logs=$member_balance_logs->with(['member']);
        $order=['add_time'=>'desc'];
        $field="*";
        $append=['add_time_format'];
        $return = DataLogic::getDataList($member_balance_logs,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function integral()
    {
        $member_id =$this->request->param('member_id/d',0);
        $nick_name=$this->request->param('nick_name');
        $mobile=$this->request->param('mobile');
        $operate_time = $this->request->param('operate_time/a');
        $where=[
            ['shop_id','=',$this->request->shopId]
        ];

        //会员判断
        $member_where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0]
        ];
        if(!empty($member_id)) $member_where[]=['member_id','=',$member_id];
        if(!empty($nick_name)) $member_where[]=['nick_name','=',$nick_name];
        if(!empty($mobile)) $member_where[]=['mobile','=',$mobile];
        $member_ids=MemberModel::where($member_where)->column('member_id') ?? [];
        $where[]=['member_id','in',$member_ids];

        //操作时间判断
        if(!empty($operate_time) && count($operate_time) == 2){
            $operate_bg_time=$operate_time[0];
            $operate_end_time=$operate_time[1];
            if(!empty($operate_bg_time) || !empty($operate_end_time)){
                if(false !== $operate_bg_time_unix = strtotime($operate_bg_time." 00:00:00")){
                    if(false !== $operate_end_time_unix = strtotime($operate_end_time." 23:59:59")){
                        $where[]=['add_time','between',[$operate_bg_time_unix,$operate_end_time_unix]];
                    }else{
                        $where[]=['add_time','>=',$operate_bg_time_unix];
                    }
                }else{
                    if(false !== $operate_end_time_unix = strtotime($operate_end_time." 23:59:59")){
                        $where[]=['add_time','<=',$operate_bg_time_unix];
                    }
                }
            }

        }

        $member_integral_logs=new MemberIntegralLogsModel();
        $member_integral_logs=$member_integral_logs->with(['member']);
        $order=['add_time'=>'desc'];
        $field="*";
        $append=['add_time_format'];
        $return = DataLogic::getDataList($member_integral_logs,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function buy()
    {
        $member_id =$this->request->param('member_id/d',0);
        $nick_name=$this->request->param('nick_name');
        $mobile=$this->request->param('mobile');
        $operate_time = $this->request->param('operate_time/a');
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_paid','=',1]
        ];

        //会员判断
        $member_where=[
            ['shop_id','=',$this->request->shopId],
        ];
        if(!empty($member_id)) $member_where[]=['member_id','=',$member_id];
        if(!empty($nick_name)) $member_where[]=['nick_name','=',$nick_name];
        if(!empty($mobile)) $member_where[]=['mobile','=',$mobile];
        $member_ids=MemberModel::where($member_where)->column('member_id') ?? [];
        $where[]=['member_id','in',$member_ids];

        //操作时间判断
        if(!empty($operate_time) && count($operate_time) == 2){
            $operate_bg_time=$operate_time[0];
            $operate_end_time=$operate_time[1];
            if(!empty($operate_bg_time) || !empty($operate_end_time)){
                if(false !== $operate_bg_time_unix = strtotime($operate_bg_time." 00:00:00")){
                    if(false !== $operate_end_time_unix = strtotime($operate_end_time." 23:59:59")){
                        $where[]=['add_time','between',[$operate_bg_time_unix,$operate_end_time_unix]];
                    }else{
                        $where[]=['add_time','>=',$operate_bg_time_unix];
                    }
                }else{
                    if(false !== $operate_end_time_unix = strtotime($operate_end_time." 23:59:59")){
                        $where[]=['add_time','<=',$operate_bg_time_unix];
                    }
                }
            }

        }

        $buy_order_model=new BuyOrderModel();
//        $buy_order_model=$buy_order_model->with(['member']);
        $order=['add_time'=>'desc'];
        $field="*";
        $append=['add_time_format','price','discount'];
        $return = DataLogic::getDataList($buy_order_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        $list=$return['list'];
        return $this->success($return);
    }
}
